package com.woniuxy.enjoylife.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.enjoylife.entity.Activity;
import com.woniuxy.enjoylife.entity.Activity_comment;
import com.woniuxy.enjoylife.entity.dto.ActivityAndActivityCommentUserDto;
import com.woniuxy.enjoylife.entity.dto.ActivityCommentAndUserDto;
import com.woniuxy.enjoylife.entity.dto.ActivityCommentUserDto;
import com.woniuxy.enjoylife.entity.dto.ActivityShowDto;
import com.woniuxy.enjoylife.mapper.UserMapper;
import com.woniuxy.enjoylife.service.ActivityCommentService;
import com.woniuxy.enjoylife.service.ActivityService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/activityComment")
public class ActivityCommentController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ActivityService activityServiceImpl;

    @Resource
    private ActivityCommentService activityCommentService;

    @Resource
    private HttpServletRequest request;


    //查询所有活动
    @RequestMapping("/pageActivityComment")
    public PageResult<ActivityShowDto> selectAllComment(int page, int size){
        int userid = (int) request.getSession().getAttribute("id");
        return activityCommentService.selectAllComment(page,size,userid);
    }

    //查找单个用户的活动(id,模糊查询)
    @RequestMapping("/pageUserComment")
    public List<Activity_comment> selectComment(int aid){
        Activity_comment activityComment = new Activity_comment();
        activityComment.getAid();
        return activityCommentService.selectComment(aid);
    }

    //新增
    @RequestMapping("/addActivityComment")
    public ResponseResult addActivityComment(Activity_comment activityComment){
        ResponseResult responseResult = new ResponseResult();
        if (activityComment.getContent() != null) {
            responseResult.success();
        } else {
            responseResult.error();
        }
        activityCommentService.addActivityComment(activityComment);
        return responseResult;
    }


    //修改
    @RequestMapping("/updateActivityComment")
    public ResponseResult updateActivityComment(Activity_comment activityComment){
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        activityCommentService.updateActivityComment(activityComment);
        return responseResult;
    }

    //删除
    @RequestMapping("/deleteActivityComment")
    public ResponseResult deleteActivityComment(int id){
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        activityCommentService.deleteActivityComment(id);
        return responseResult;
    }


    /**
     * 请求活动评论
     */
    @PostMapping("/allComments")
    public List<Activity_comment> AllComment(){

        List<Activity_comment> activityComments = activityCommentService.list();


        return activityComments;
    }

    //平台查询所有活动评论及其发表人
    @RequestMapping("/findAllActivityCommentAndUser")
    public PageResult<List<ActivityAndActivityCommentUserDto>> findAllActivityCommentAndUser(String content,int current, int size){
        PageResult<List<ActivityAndActivityCommentUserDto>> pageResult;
        //如果有搜索，调用模糊搜索方法
        if(content != null && content != ""){
            pageResult = activityCommentService.findLikeAllAndUser(content, current, size);
        }else {
            pageResult = activityCommentService.findAllActivityCommentAndUser(current, size);
        }
        return pageResult;
    }

    //平台模糊查询所有人的评论
//    @RequestMapping("/findLikeAllAndUser")
//    public PageResult<List<ActivityAndActivityCommentUserDto>> findLikeAllAndUser(String content, int current, int size){
//        return activityCommentService.findLikeAllAndUser(content, current, size);
//    }

    //*****************用户活动*************************
    @GetMapping("findAllactivityComment/{id}")
    public ResponseResult findAllactivityComment(@PathVariable("id") Integer id){
        ResponseResult responseResult = new ResponseResult();

        List<ActivityCommentAndUserDto> activityComments = activityCommentService.findActivityCommentAndReply(id);
responseResult.setData(activityComments);
responseResult.success();
        return responseResult;
    }
//    回复评论
@GetMapping("addActivityComments/{comment}/{aid}")
    public ResponseResult addActivityComment(@PathVariable("comment") String content,@PathVariable("aid") Integer aid){
        ResponseResult responseResult = new ResponseResult();
        Integer uid = (Integer)request.getSession().getAttribute("id");
        Activity_comment activity_comment = new Activity_comment().setContent(content).setUid(uid).setTime(LocalDateTime.now()).setAid(aid).setStatus(0);
        boolean reslut = activityCommentService.saveOrUpdate(activity_comment);
        if (reslut){
            responseResult.success();
        }else {
            responseResult.error();
        }
        return responseResult;
    }

//    使用活动id查询评论
@GetMapping("findCommentByAid/{aid}")
public ResponseResult findCommentByAid(@PathVariable("aid") Integer aid){
    ResponseResult responseResult = new ResponseResult();
            QueryWrapper wrapper = new QueryWrapper();
     wrapper.eq("aid", aid);
    List list = activityCommentService.list(wrapper);
            responseResult.setData(list);
    if (list!=null){
        responseResult.success();
    }else {
        responseResult.error();
    }
    return responseResult;
}

}
